package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.DateVO;
import com.sky.vo.TopVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    int insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 修改订单状态!!!
     * @param orderStatus 订单状态
     * @param orderPaidStatus 订单支付状态
     * @param checkOutTime 订单结账时间
     * @param id 订单id
     */
    @Update("update orders set status = #{orderStatus}, pay_status = #{orderPaidStatus}, checkout_time = #{checkOutTime} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, Long id);

    /**
     * 历史订单查询(分页查询)
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageOrderQuery(OrdersPageQueryDTO ordersPageQueryDTO);
    /**
     * Admin个个状态订单数量统计!
     * @return
     */
    List<Orders> countStatus();

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);
    /**
     * spring Task 处理超时订单
     * @param status 订单状态
     * @param time 当前系统时间
     * @return
     */
    @Update("update orders set status=6,cancel_reason=#{str},orders.cancel_time=now() where status=#{status} and order_time<=#{time}")
    void updateStatusByTime(Integer status, LocalDateTime time,String str);
    /**
     * 处理一直在拍送中的订单
     * @param status 订单状态
     * @return
     */
    @Update("update orders set status=5,delivery_time=now() where status=#{status}")
    void deliveryOrder(Integer status);
    /**
     *订单数量统计
     * @param dateVO
     * @return
     */
    @Select("select count(*) from orders where order_time between #{begin} and #{end}")
    Integer getTurnoverReportList(DateVO dateVO);

    /**
     * 订单有效数量统计
     * @param dateVO
     * @return
     */
    @Select("select count(*) from orders where order_time between #{begin} and #{end} and status=5")
    Integer validOrderCountList(DateVO dateVO);

    /**
     * 有效订单数量统计
     * @param dateVO
     * @return
     */
    @Select("select count(*) from orders where status=5 and order_time between #{begin} and #{end}")
    Integer validOrderCount(DateVO dateVO);

    /**
     * 销量排名前10
     * @param dateVO
     * @return
     */
    List<TopVO> getSalesTop10(DateVO dateVO);

    /**
     * 订单数量统计
     * @return
     */
    @Select("select count(*) from dish")
    Integer countOrders();

    /**
     * 订单状态统计
     * @return
     */
    @Select("select count(*) from orders where status=#{status}")
    Integer getOrderStatusCount(Integer status);
}
